package 集合;

/**
 * https://leetcode-cn.com/problems/intersection-of-two-arrays/
 */
public class _349_两个数组的交集 {
    public int[] intersection(int[] nums1, int[] nums2) {
        int[] ints = null;
        if (nums1 == null || nums2 == null) return ints;
        java.util.Set<Integer> set1 = new java.util.HashSet<>();
        java.util.Set<Integer> set2 = new java.util.HashSet<>();

        for (int i : nums1) {
            set1.add(i);
        }
        for (int i : nums2) {
            if (set1.contains(i)) set2.add(i);
        }
        ints = new int[set2.size()];
        int index = 0;
        for (Integer result : set2) {
            ints[index] = result;
            index++;
        }
        return ints;
    }
}
